$(function(){
	
//	window.onbeforeunload = function(){
//		return '您确定要放弃本此考试吗？';
//    };
	$('input').iCheck({
		checkboxClass: 'icheckbox_square-green',
		radioClass: 'iradio_square-green',
		increaseArea: '-10' // optional
	});
	var editors;
	KindEditor.ready(function(K) {
		editors = K.create('.question_content', {	
			resizeType : 1,
			//allowPreviewEmoticons : false,
			afterBlur: function () { this.sync(); },
			allowImageUpload : true,
			width : "100%", 
			allowUpload : true,
			minHeight:120,
			uploadJson : '/upload/images?dest_dir=questions',
			items : ["justifyleft","image"]
		});
	});

	$(document).on('click','.finish_question',function(){
		$(".tan-modal .tan-modal-content ul").html();
	    $(".nl_t3").css("z-index",2);
		$(".tan-modal").addClass("dn");	
		var uri = $(this).attr('data-value');
		var type = $(this).attr('data-type');
		var isNull = true;
		var parentNode = $(this).parent().parent();
		parentNode.find('input[name^=user_answer]').each(function(val){
			if($(this)[0].checked === true){
				isNull = false;
			}
		});
		if(isNull){
			layer.confirm('您还没有给出答案，确定要放弃本题吗?',{title:'确认要放弃'},function(result){
				if(result){
					dosubmit('question_form',uri,type);
				}
			});
		} else {
			dosubmit('question_form',uri,type);
		}
	});
	
	$(document).on('click','.next_question',function(){
		var parentNode = $(this).parent().parent();
		var isNull = true;
		parentNode.find('input[name^=user_answer]').each(function(val){
			if($(this)[0].checked === true){
				isNull = false;
			}
		});
		if(parentNode.find('textarea[name^=user_answer]').hasClass('question_content')){
			isNull = false;
		}
		if(isNull){
			layer.confirm('您还没有给出答案，确定要放弃本题吗?',{title:'确认要放弃'},function(result){
				if(result){
					layer.msg('您可以通过上一题或题库面板选择回到上一题');
					parentNode.hide();
					parentNode.next().show();
				}
			});
		}else{
			parentNode.hide();
			parentNode.next().show();
		}
	});
	$(document).on('click','.previous_question',function(){
		var parentNode = $(this).parent().parent();
		parentNode.hide();
		parentNode.prev().show();
	});
	$(document).on('click','.jump_to_question',function(){
		var id = $(this).attr('data-value');
		$('#question_'+id).siblings().hide();
		$('#question_'+id).show();
		$(".tan-modalz").addClass("dn");
	});
	$(document).on('click','.check_interview_time',function(){
		$("#select_interview_time").removeClass("dn");
	});
	$(document).on('click','.submit_interview', function(){
		var back_url = $('input[name=back_url]').val();
		var form = $('#submit_interview_form');
		var data = form.serializeArray();
		var params = {
				url: '/tests/interview-store',
				type: 'POST',
				dataType: 'json',
				data: data,
		};
		$.ajax(params).done(function(resp){
			layer.msg('您已确认面试时间，页面即将自动跳转...');
			if(back_url != '' && typeof(back_url) != 'undefined'){
				window.location.href = back_url;
			} else {
				window.opener=null;
				window.open('','_self');
				window.close();
			}
		}).fail(function(erro){
			error(erro);
		});
	});
});
var h=$(".nl_t3").offset().top;   
function Sublist(){
	var hs=$(document).scrollTop();
	var hz=h-hs+99+19;
	var has=$(".tan-modal").hasClass("dn");
	if(has){
		$(".nl_t3").css("z-index",99999);
		$(".tan-modal .tan-modal-dialog").css("margin-top",hz);	
		$(".tan-modal").removeClass("dn");
	}else{
		$(".tan-modal-content ul").html();
		$(".nl_t3").css("z-index",2);				
	    $(".tan-modal").addClass("dn");				
	}
}

$(window).scroll(function(){
	var has=$(".tan-modal").hasClass("dn");
	if(!has){
		var hs=$(document).scrollTop();
	    var hz=h-hs+99+19;
		$(".tan-modal .tan-modal-dialog").css("margin-top",hz);	
	}			
});
function dosubmit(formId,uri,type){
	var data = $('#'+formId).serializeArray();
	var params = {
			url: uri,
			type: 'POST',
			dataType: 'json',
			data: data,
	};
	var isNull = true;
   $('#'+formId).find('input[name^=user_answer]').each(function(val){
		if($(this)[0].checked === true){
			isNull = false;
		}
	});
   if(isNull){
		layer.confirm('您还有题目没有给出答案，确认交卷吗?',{title:'确认交卷'},function(result){
			if(result){
				layer.msg('试卷提交中...');
				submitAjax(params,type);
			}
		});
	}else{
		submitAjax(params,type);
	}
}
function dosubmitAuto(formId,uri){
	var data = $('#'+formId).serializeArray();
	var uri = $('#'+formId+' .finish_question').attr('data-value');
	var type = $('#'+formId+' .finish_question').attr('data-type');
	var params = {
			url: uri,
			type: 'POST',
			dataType: 'json',
			data: data,
	};
	var isNull = true;
   $('#'+formId).find('input[name^=user_answer]').each(function(val){
		if($(this)[0].checked === true){
			isNull = false;
		}
	});
	submitAjax(params,type);
}
function submitAjax(params,type){
	var back_url = $('input[name=back_url]').val();
	var nextexam = $('input[name=next]').val();
	var nid = $('input[name=need_id]').val();
	$.ajax(params).done(function(repo){
		$('#exam-content').remove();
		$('#finished-exam-content').show();
		$('#clock').countdown('pause');
		if(repo.status=="success"){
			if(type=='system') {
				if(nextexam == 1 || nextexam == '1'){
					layer.closeAll();
					layer.open({
						type: 1,
						title:false,
						btn: ['忽略', '下一步：性格测试'],
						closeBtn: 1,
						shift: 2,
						shadeClose: true, //开启遮罩关闭
						area: ['320px', '200px'], //宽高
						content: '<div style="width:80%;margin:40px auto;line-height:20px;">恭喜您，通过系统能力测试，你可以在考试历史中进行查看...</div>',
						end: function(){ window.location.href="/member/need"; },
						btn1: function(index, layero){
							window.location.href="/member/need";
						},
						btn2:function(index,layero){
							window.open("/employ/2406?nid="+nid);
						}
					});
				}else{
					if(back_url != '' && typeof(back_url) != 'undefined'){
						window.location.href = back_url;
					} else {
						window.opener=null;
						window.open('','_self');
						window.close();
					}
				}
			} else {
				$('input[name=exam_history_id]').val(repo.data.id);
				var html='<p>恭喜您 成功通过企业测试  请去选择面试时间！</p>';
				var html1='<a class="confirm check_interview_time" href="javascript:void(0)">选择面试时间</a>';
				$("#tan-modal1 .tan-modal-content").html(html);
				$("#tan-modal1 .tan-modal-actions").html(html1);
				$("#tan-modal1").removeClass("dn");
			}
		}else if(repo.status == 'nopass'){
			if(type=='system') {
				if(nextexam == 1 || nextexam == '1'){
					layer.closeAll();
					layer.open({
						type: 1,
						title:false,
						btn: ['前往学习', '下一步：性格测试'],
						closeBtn: 0,
						shift: 2,
						shadeClose: true, //开启遮罩关闭
						area: ['320px', '200px'], //宽高
						content: '<div style="width:80%;margin:40px auto;line-height:40px;">恭喜您完成系统能力测试，但是您的分数不够理想，您可以点击前往学习按钮进行学习</div>',
						btn1: function(index, layero){ 
							window.location.href="/teaches/list/1";
						},
						btn2:function(index,layero){
							window.location.href="";
						}
					});
				}else{
					layer.open({
						type: 1,
						title:false,
						btn: ['忽略', '前往学习'],
						closeBtn: 0,
						shift: 2,
						shadeClose: true, //开启遮罩关闭
						area: ['320px', '200px'], //宽高
						content: '<div style="width:80%;margin:40px auto;line-height:40px;">恭喜您完成系统能力测试，但是您的分数不够理想，您可以点击前往学习按钮进行学习</div>',
						btn1: function(index, layero){ 
							if(back_url != '' && typeof(back_url) != 'undefined'){
								window.location.href = back_url;
							} else {
								window.opener=null;
								window.open('','_self');
								window.close();
							}
						},
						btn2:function(index,layero){
							window.location.href="/teaches/list/1";
						}
					});
				}
			} else {
				var html='<p>抱歉 您在测试中仅超越 '+repo.data.surm+'% 未能通过企业测试！<br>可以选择在<a href="/teaches/list/1">充电加油</a>继续努力哦！</p>'; 
				$("#tan-modal1 .tan-modal-content").html(html);
				$("#tan-modal1").removeClass("dn");
			}
		}else{
			var html='<p>'+repo.msg+'</p>'; 
			$("#tan-modal1 .tan-modal-content").html(html);
			$("#tan-modal1").removeClass("dn");
		}
	}).fail(function(erro){
		error(erro);
	});
}

//关闭按钮
function Close(){
	$("#tan-modal1 .tan-modal-content").html();
	$("#tan-modal1 .tan-modal-actions").html();
	$(".tan-modalz").addClass("dn");		
}

//空白关闭
$(document).mouseup(function(e){
	var _con = $('.tan-modal-dialog,.nl_t3');
	if(!_con.is(e.target) && _con.has(e.target).length === 0){
		$(".tan-modal .tan-modal-content ul").html();
		$(".nl_t3").css("z-index",2);
		$("#tan-modal1 .tan-modal-content").html();
		$("#tan-modal1 .tan-modal-actions").html();
		$(".tan-modalz").addClass("dn");		
	}
});	
